"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const koa_1 = __importDefault(require("koa"));
const child_process_1 = require("child_process");
const colors_console_1 = __importDefault(require("colors-console"));
const configTool_1 = __importDefault(require("./com/configTool"));
const clientMiddleWare_1 = __importDefault(require("./middleware/clientMiddleWare"));
const manaMiddleWare_1 = __importDefault(require("./middleware/manaMiddleWare"));
const webSocketTool_1 = __importDefault(require("./com/webSocketTool/webSocketTool"));
const portIsOccupied_1 = __importDefault(require("./lib/System/portIsOccupied"));
const getIPAddr_1 = __importDefault(require("./lib/System/getIPAddr"));
const WS = require('ws');
const clientAPP = new koa_1.default();
const manageAPP = new koa_1.default();
async function main() {
    (0, child_process_1.exec)('reg add HKEY_CURRENT_USER\\Console /v QuickEdit /t REG_DWORD /d 00000000 /f');
    await configTool_1.default.init();
    const clientServerPort = configTool_1.default.baseConfig.clientServerPort;
    const manageServerPort = configTool_1.default.baseConfig.manageServerPort;
    const wsServerPort = configTool_1.default.baseConfig.wsServerPort;
    if (clientServerPort !== await (0, portIsOccupied_1.default)(clientServerPort, false)) {
        console.log((0, colors_console_1.default)('red', `============ 端口号 ${clientServerPort} 被占用，程序启动失败 ============`));
        return;
    }
    if (manageServerPort !== await (0, portIsOccupied_1.default)(manageServerPort, false)) {
        console.log((0, colors_console_1.default)('red', `============ 端口号 ${manageServerPort} 被占用，程序启动失败 ============`));
        return;
    }
    const ipAddr = (0, getIPAddr_1.default)();
    await new Promise((resolve, reject) => {
        clientAPP.listen(clientServerPort, function () {
            console.log((0, colors_console_1.default)('cyan', `【静态资源网站资源目录】：${configTool_1.default.wwwPath}`));
            console.log((0, colors_console_1.default)('magenta', `【启动 HTTP  Web服务器】：URL:【http://${ipAddr}:${clientServerPort}】`));
            (0, clientMiddleWare_1.default)(clientAPP);
            resolve(null);
        });
    });
    await new Promise((resolve, reject) => {
        if (manageServerPort) {
            const manageServer = manageAPP.listen(manageServerPort, function () {
                const manageMessage = `【启动 管理员 接口服务】：URL:【http://${ipAddr}:${manageServerPort}】`;
                if (wsServerPort === manageServerPort) {
                    const wss = new WS.Server({ server: manageServer });
                    webSocketTool_1.default.start(wss);
                    console.log((0, colors_console_1.default)('magenta', manageMessage + `与【WebSocket】共用【${manageServerPort}】端口`));
                }
                else {
                    const wsAPP = new koa_1.default();
                    const wsServer = wsAPP.listen(wsServerPort, function () {
                        const wss = new WS.Server({ server: wsServer });
                        webSocketTool_1.default.start(wss);
                        console.log(manageMessage + `；【启动 WebSocket 服务】：占用端口:【${wsServerPort}】`);
                    });
                }
                (0, manaMiddleWare_1.default)(manageAPP);
                resolve(null);
            });
        }
    });
    console.log((0, colors_console_1.default)('green', `============ 服务器启动成功！当前版本：${configTool_1.default.baseConfig.version} ============`));
}
main().then();
